Skip to content

Conversation

clementblaudeau
Copy link
Contributor

This PR improves lean support for constants, using the FunctionToConstants resugaring. It removes the ad-hoc treatment of litteral consts.

Overview

A rust const can contain possibly panicking computation, like:

const C3: u32 = if true { 890 } else { 9 / 0 };

However, this is computed at compile time by rustc, so we can expect panic-freedom at extraction-time. In the Lean prelude, we add the two functions:

def isOk {α : Type} (x: Result α) : Bool := match x with
| .ok _ => true
| _ => false

def of_isOk {α : Type} (x: Result α) (h: Result.isOk x): α :=
  by cases x <;> try simp_all <;> assumption

The extracted code hardcodes a proof of panic-freedom by computation (by rfl), which should always work:

def Lean_tests.Constants.C3 :  u32 :=
  Result.of_isOk
  (do if true then pure (890 : u32) else (← (9 : u32) /? (0 : u32)))
  (by rfl)

Closes #1614

@clementblaudeau clementblaudeau added this to the Lean backend v1.0 milestone Oct 9, 2025
@clementblaudeau clementblaudeau requested a review from a team as a code owner October 9, 2025 10:35
@clementblaudeau clementblaudeau added the backend Issue in one of the backends (i.e. F*, Coq, EC...) label Oct 9, 2025
@clementblaudeau clementblaudeau added proof-lib Issues related the backend-specific definitions (in the proof-lib folder) lean Related to the Lean backend or library labels Oct 9, 2025
Copy link
Contributor

@maximebuyse maximebuyse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, only one comment.

body: _,
params: _,
safety: _,
} if name.is_anonymous_const() => false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this case? If yes you can factor them with a disjunctive pattern

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Issue in one of the backends (i.e. F*, Coq, EC...) lean Related to the Lean backend or library proof-lib Issues related the backend-specific definitions (in the proof-lib folder)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use a resugaring to identify literal consts

2 participants